﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utils;


namespace UI.ModSalary
{
    public partial class JjProcess : System.Web.UI.Page
    {
        string proCode = string.Empty;
        JjProcBiz jjProcBiz = new JjProcBiz();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["ProCode"] != null)
            {
                proCode = Request.Params["ProCode"].ToString();
            }
            else
            {
                JSUtility.Alert(this,"参数错误");
                return;
            }
            if (!IsPostBack)
            {
                DataBond(proCode);
            }
        }
        private void DataBond(string proCode)
        {
            DataTable dt = jjProcBiz.GetLists(proCode);
            gvList.DataSource = dt;
            gvList.DataBind();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string procCode = txtProcCode.Text.Trim();
            string procName = txtName.Text.Trim();
            if (string.IsNullOrWhiteSpace(procCode) || string.IsNullOrWhiteSpace(procName))
            {
                JSUtility.Alert(this, "工序编号和名称不能为空！");
                return;
            }
            if (jjProcBiz.IsExist(procCode))
            {
                JSUtility.Alert(this, "工序编号已经被使用，请更换一个！");
                return;
            }
            string price = txtPrice.Text.Trim();
            string scrapSal = txtScrapSal.Text.Trim();
            if (!RegexRule.IsNumeric(price) || !RegexRule.IsNumeric(scrapSal))
            {
                JSUtility.Alert(this, "价格必须填写数字！");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ProcCode", procCode);
            sdList.Add("ProCode", proCode);
            sdList.Add("Name", procName);
            sdList.Add("Price", price);
            sdList.Add("ScrapSal", scrapSal);
           
            if (jjProcBiz.Insert(sdList))
            {
                JSUtility.Alert(this, "增加成功！");
                DataBond(proCode);
            }
            else
                JSUtility.Alert(this, "增加失败！");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string procCode = txtProcCode.Text.Trim();
            string procName = txtName.Text.Trim();
            if (string.IsNullOrWhiteSpace(procCode) || string.IsNullOrWhiteSpace(procName))
            {
                JSUtility.Alert(this, "工序编号和名称不能为空！");
                return;
            }
            if (!jjProcBiz.IsExist(procCode))
            {
                JSUtility.Alert(this, "工序编号不存在！");
                return;
            }
            string price = txtPrice.Text.Trim();
            string scrapSal = txtScrapSal.Text.Trim();
            if (!RegexRule.IsNumeric(price) || !RegexRule.IsNumeric(scrapSal))
            {
                JSUtility.Alert(this, "价格必须填写数字！");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ProcCode", procCode);
            sdList.Add("ProCode", proCode);
            sdList.Add("Name", procName);
            sdList.Add("Price", price);
            sdList.Add("ScrapSal", scrapSal);

            if (jjProcBiz.Update(sdList))
            {
                JSUtility.Alert(this, "更新成功！");
                DataBond(proCode);
            }
            else
                JSUtility.Alert(this, "更新失败！");
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            string procCode = txtProcCode.Text.Trim();
            if (jjProcBiz.Delete(procCode))
            {
                JSUtility.Alert(this, "删除成功！");
                DataBond(proCode);
            }
            else
                JSUtility.Alert(this, "删除失败！");
        }
    }
}